<?php
if(!isset($AppUI->id) || $AppUI->id <=0 || ($AppUI->id > 0 && $AppUI->type < ROLE_LEADER)) {
	redirect(HOST_BACKEND);
}
$this->load->model('section_model', 'section');	
$this->load->model('permission_model', 'permission');
$intAdminId     = $this->input->post('padminid', 0);
$strUsername    = $this->input->post('pusername', null);
$arrPerms		= null;
$sectionList    = $this->section->getSection();
if(isset($sectionList)) {
   foreach($sectionList as $row) {
        $intPerm             = $row['sectionid'];
        $arrPerm             = $this->input->post('pPerm' . $intPerm, null);        
		$arrPerms[$intPerm]  = CI_Function::convertArrToInt($arrPerm);
	} 
}
if(isset($arrPerms)) {	
	$intError	= $this->permission->delete($AppUI->id, $intAdminId);
	if($intError == 0) {
		foreach($arrPerms as $key=>$value) {
			if($value > -1) {
				$intError = $this->permission->add($AppUI->id, $intAdminId, $key, $value);					
				if($intError > 0) {					
					break;
				}
			}
		}
	}
}
if($intError > 0) CI_Function::setError($arrError['no_permission']);
else {
	$strMessage	= $arrMessage['permission_sucessfully'];
	$strMessage	= str_replace('{0}', $strUsername, $strMessage);
	CI_Function::setMsg($strMessage);
}
redirect(HOST_BACKEND . 'permission?adminid=' .$intAdminId);